﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Summerset.Isbndb
{
    /// <summary>
    /// Allows enumeration through the Pages in a PagesCollection.
    /// </summary>
    /// <typeparam name="PageType">The type of page that will be contained in the PagesCollection.</typeparam>
    /// <typeparam name="ElementType">The element contained in each page.</typeparam>
    public sealed class PagesEnumerator<PageType, ElementType> : IEnumerator<PageType> 
        where PageType : ResponsePageBase<ElementType> 
        where ElementType : ResponseElementBase
    {
        #region Constructor + Fields

        PagesCollection<PageType, ElementType> _source;
        int _position = -1;

        /// <summary>
        /// Initializes a new instance of the ResponsePagesEnumerator class.
        /// </summary>
        /// <param name="source">The source.</param>
        public PagesEnumerator(PagesCollection<PageType, ElementType> source)
        {
            _source = source;
        }

        #endregion
        #region IEnumerator<T> Members

        /// <summary>
        /// Gets page at the enumerator's current position.
        /// </summary>
        public PageType Current
        {
            get { return _source[_position]; }
        }

        #endregion
        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            _source = null;
        }

        #endregion
        #region IEnumerator Members

        /// <summary>
        /// Returns the current instance at the enumerators position.
        /// </summary>
        object System.Collections.IEnumerator.Current
        {
            get { return this.Current; }
        }

        /// <summary>
        /// Moves to the next element in the collection.
        /// </summary>
        /// <returns><c>true</c> if there are more elements in the collection.  <c>false</c> if no more elements in the collection.</returns>
        public bool MoveNext()
        {

            if (++_position >= _source.Count)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Moves the enumerator back to a position just before the start of the collection.
        /// </summary>
        public void Reset()
        {
            _position = -1;
        }

        #endregion
    }
}
